<?php
//打印数据
function p($data)
{
	echo "<pre>";
	var_dump($data);
	"</pre>";
}

//配置文件  C('a',null)
function C($name='',$value='')
{
	static $conf=[];
	if(empty($name))
	{
		return $conf;
	}
	else if(is_array($name))
	{
		$conf=array_merge($conf,$name);
	}
	else if(is_string($name))
	{
		if(is_null($value))
		{
			if(isset($conf[$name]))
			{
				unset($conf[$name]);
			}
			return true;
		}
		else if($value=='')
		{
			return isset($conf[$name])?$conf[$name]:null;
		}
		else
		{
			return $conf[$name]=$value;
		}
	}
}

/**
	 * 生成URL地址
	 * @param [string] $path   [模块.控制器.方法]
	 * @param [array] $params [get参数]
	 */
	function U($path,array $params=[])
	{
		$info = explode('.', $path);
		 switch (count($info)) {
		 	case 3:
		 		$url ="?m={$info[0]}&c={$info[1]}&a={$info[2]}";
		 		break;
		 	case 2:
		 		$url ="?m=".MODULE."&c={$info[0]}&a={$info[1]}";
			 	break;
			case 1:
		 		$url ="?m=".MODULE."&c=".CONTROLLER."&a={$info[0]}";
			 	break;

		 }
		 if(!empty($params))
		 {
		 	$args = '';
		 	foreach($params as $key=>$value)
		 	{
		 		$args.='&'.$key.'='.$value;
		 	}
		 	$url.=$args;
		 }
		 return $url;
	}


//调用api 
//api('user/get',[1])
function api($action,array $params=[])
{
	$info =explode('/', $action);
	$class = 'Common\\Api\\'.ucfirst($info[0]);
	return call_user_func_array([new $class,$info[1]], $params);
}

/**
 * 缓存控制
 * @param [type]  $name   [description]
 * @param string  $value  [description]
 * @param integer $expire [description]
 */
function S($name,$value='',$expire=3600)
{
	static $cache=[];
	//取缓存
	if($value==='')
	{
		//从静态变量取
		if(isset($cache[$name]))
		{
			return $cache[$name];
		}
		else
		{
			//从缓存文件取
			if($d = (new \Framework\Tool\Cache)->get($name))
			{
				return $cache[$name]=$d;	
			}
		}
	}
	else
	{
		//设置缓存
		if((new \Framework\Tool\Cache)->set($name,$value,$expire))
		{
			$cache[$name]=$value;
			return true;
		}
	}
}

function _404()
{
	require C('tpl_404');
}
















